package com.cykj.pos.mapper;

import com.baomidou.mybatisplus.annotation.InterceptorIgnore;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.cykj.pos.domain.*;
import com.cykj.pos.domain.dto.*;
import com.cykj.pos.domain.vo.BizTransactionAmountEntityVO;
import com.cykj.pos.domain.vo.WalletVO;
import com.cykj.pos.profit.dto.ActivateReachDTO;
import com.cykj.pos.profit.dto.MerchantInfoDTO;
import com.cykj.pos.profit.dto.TransDataDTO;
import com.cykj.pos.profit.dto.TransactionsDTO;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * 商户交易记录Mapper接口
 *
 * @author ningbingwu
 * @date 2021-01-29
 */
public interface BizMerchTransactionsMapper extends BaseMapper<BizMerchTransactions> {
    /**
     * 查询当月交易额
     *
     * @param merchantInfoDTO
     * @return
     */
    TransactionsDTO queryNowMonthDeal(MerchantInfoDTO merchantInfoDTO);

    /**
     * @author: haoran
     * @date: 2022/7/28
     * 方法名称： 所有交易记录列表
     */
    List<BizMerchTransactions> queryLists(BizMerchTransactionsDTO bizMerchTransactionsDTO);

    /**
     * @author: haoran
     * @date: 2022/7/29
     * 方法名称： 交易记录
     */
    List<BizMerchTransactions> listsPartner1(BizMerchTransactionsDTO bizMerchTransactionsDTO);

    BizMerchBill b1amount(BizMerchTransactions bizMerchTransactions);

    BizMerchBill b2amount(BizMerchTransactions bizMerchTransactions);

    BizMerchBill b3amount(BizMerchTransactions bizMerchTransactions);

    List<BizMerchTransactions> listsPartner(BizMerchTransactions bizMerchTransactions);

    List<BizMerchTransactions> listsAsscend(BizMerchTransactions bizMerchTransactions);

    List<BizMerchTransactions> listsReturn(BizMerchTransactionsDTO bizMerchTransactionsDTO);

    List<BizFeesChargedDTO> tramcBilling(BizFeesChargedDTO bizFeesChargedDTO);

    List<BizProfitDeatail> queryListAlls(Long uuid);

    List<CashbackRecordDTO> listReturnAlls(Long uuid);

    List<CashbackRecordDTO> listTramcBillingAlls(Long uuid);

    StealSwitchDTO listStealSwitch(StealSwitchDTO stealSwitchDTO);

    AscendSwitchDTO listAscendSwitch(AscendSwitchDTO ascendSwitchDTO);

    int  stealupdate(StealSwitchDTO stealSwitchDTO);

    int  ascendupdate(AscendSwitchDTO ascendSwitchDTO);

    int  estimateIsPass(EstimateIsPassDTO estimateIsPassDTO);

    int  estimateUpdatePass(EstimateIsPassDTO estimateIsPassDTO);

    List<BizMerchTransactions> listss();

    int updatalists(BizMerchTransactions bizMerchTransactions);

    BizMerchTransactions getErrorCause(Long Id);

    /**
     * 查询所有交易额
     *
     * @param merchantInfoDTO
     * @return
     */
    TransactionsDTO queryAllMonthDeal(MerchantInfoDTO merchantInfoDTO);

    /**
     * 日维度个人交易额查询
     *
     * @param transPerformanceDTO
     * @return
     */
    List<TransPerformanceDTO> selectMerchantDayTransactionInfo(TransPerformanceDTO transPerformanceDTO);

    /**
     * 月维度个人交易额查询
     *
     * @param transPerformanceDTO
     * @return
     */
    List<TransPerformanceDTO> selectMerchantMonthTransactionInfo(TransPerformanceDTO transPerformanceDTO);

    /**
     * 月维度团队日交易额
     *
     * @param transPerformanceDTO
     * @return
     */
    List<TransPerformanceDTO> selectTeamDayTransactionInfo(TransPerformanceDTO transPerformanceDTO);

    /**
     * 月维度团队月交易额
     *
     * @param transPerformanceDTO
     * @return
     */
    List<TransPerformanceDTO> selectTeamMonthTransactionInfo(TransPerformanceDTO transPerformanceDTO);

    /**
     * 总日交易额
     *
     * @param transPerformanceDTO
     * @return
     */
    List<TransPerformanceDTO> selectAllDayTransactionInfo(TransPerformanceDTO transPerformanceDTO);

    /**
     * 总月交易量
     *
     * @param transPerformanceDTO
     * @return
     */
    List<TransPerformanceDTO> selectAllMonthTransactionInfo(TransPerformanceDTO transPerformanceDTO);

    /**
     * 查询商户交易笔数
     *
     * @param id
     * @return
     */
    List<BizMerchTransactions> queryDealNum(@Param("id") String id, @Param("tableName") String tableName);

    /**
     * 按方式 查询月交易额
     *
     * @param merchantInfoDTO
     * @return
     */
    TransactionsDTO queryNowMonthDealWay(MerchantInfoDTO merchantInfoDTO);

    /**
     * 微信公众号激活达标
     *
     * @param activateReachDTO
     * @return
     */
    ActivateReachDTO queryActivatePosMonthAmount(ActivateReachDTO activateReachDTO);

    /**
     * 保存盛付通交易记录
     *
     * @param merchTransaction
     * @return
     */
    int saveBySftTransaction(BizMerchTransactions merchTransaction);

    /**
     * 盛付通根据订单id更新交易记录
     *
     * @param sftSettleDTO
     * @return
     */
    int updateTransactionByOrderId(SftSettleDTO sftSettleDTO);

    /**
     * 根据订单id查询盛付通交易记录
     *
     * @param sftSettleDTO
     * @return
     */
    BizMerchTransactions selectTransactionByOrderId(SftSettleDTO sftSettleDTO);

    /**
     * 根据盛付通businessType=5里订单号查询交易记录
     *
     * @param orderNo
     * @return
     */
    BizMerchTransactions selectTransactionByInnerMerchCode(String orderNo);

    /**
     * 更新discount_rate_flag字段值是否+3
     *
     * @param innerMerchCode
     */
    int updateTransactionByFee(String innerMerchCode);

    Long findMerchId(Long userId);

    List<TransactionRecordDTO> findTransactionRecord(TransactionRecord transactionRecord);

//    Integer total(@Param("merchId") Long merchId, @Param("startTime") String startTime,
//                  @Param("endTime") String endTime, @Param("pageNum") Integer pageNum,
//                  @Param("pageSize") Integer pageSize);

    @Select("SELECT profit_amount, wallet_amount, secret_key FROM biz_wallet WHERE user_id = 50")
    WalletVO getWallte();

    @Update("UPDATE biz_wallet set profit_amount = #{profitAmountEncrypt}, wallet_amount = #{walletAmountEncrypt} WHERE user_id = 50")
    void updateWallet(@Param("profitAmountEncrypt") String profitAmountEncrypt, @Param("walletAmountEncrypt") String walletAmountEncrypt);

    @Insert("INSERT INTO biz_rate_money (sn_code, amount, addi_amount, coin, acq_name, create_time, fee_name) " +
            "VALUES(#{posSn}, #{amount}, #{addiAmount}, #{take}, #{acqName}, now(), #{feeName} )")
    void insetRateMoney(@Param("posSn") String posSn, @Param("amount") String amount, @Param("addiAmount") String addiAmount,
                        @Param("take") String take, @Param("acqName") String acqName,
                        @Param("feeName") String feeName);

    @Select("SELECT count(*) FROM biz_transaction_amount WHERE pos_sn = #{posSn} and amnount = #{amount} and host_date = #{hostDate} and trans_type=#{transType}")
    Integer findTransaction( @Param("posSn") String posSn, @Param("amount") String amount, @Param("hostDate") String hostDate,@Param("transType")String transType);

    /**
     * xxl交易记录表插偷单记录
     *
     * @param bizMerchTransactions
     * @return
     */
    int insertbizMerchTransactions(BizMerchTransactions bizMerchTransactions);

    /**
     * 获取sn所有交易记录
     * @param snCode
     * @return
     */
    List<BizMerchTransactions> selectBizMerchTransactions(String snCode);

    /**
     * ba记录换成虚拟的
     * @param snCode
     */
    void updateBizMerchTransactionsBySN(@Param("snCode") String snCode,@Param("fictitiousSnCode") String fictitiousSnCode);

    /**
     * 添加解绑交易记录表
     * @param list
     */
    void insertbizMerchTransactionsUnbund( BizMerchTransactions list);

    /**
     * 获得钱包
     * @param userId
     * @return
     */
    BizWallet getBizWallte(Long userId);

    @Update("UPDATE biz_wallet set profit_amount = #{profitAmountEncrypt}, wallet_amount = #{walletAmountEncrypt} WHERE user_id = #{userId}")
    void updateWalletCoin(@Param("profitAmountEncrypt") String profitAmountEncrypt,@Param("walletAmountEncrypt") String walletAmountEncrypt,@Param("userId") Long userId);

    /**
     * 查询交易量和交易笔数
     * @param sn
     * @return
     */
    BizTransactionCountDTO selectCountBizMerchTransactions(@Param("sn")String sn, @Param("tableName")String tableName);
    String selectSumAmout(CountBizMerchTransactions countBizMerchTransactions);

    String selectSuffix(Long userId);
    Long selectMerchId(Long userId);

    BizTransactionCountDTO selectSumCountBizMerchTransactions(@Param("format") String format,@Param("sn") String sn, @Param("tableName") String tableName);
    String selectMouthAmount(Long merchId);

    @Select("SELECT * FROM biz_profit_deatail where transaction_amount_id = #{uuid} ")
    List<BizProfitDeatail> selectBizProfitDeatail(String uuid);

    @Select("SELECT count(*) FROM biz_profit_deatail where transaction_amount_id = #{uuid} ")
    int selectBizProfitDeatailCount(String uuid);

    @Select("SELECT count(*) FROM biz_service_bills where uuid= #{uuid} ")
    int selectBizProfitDeatailCount291(String uuid);

    @Select("SELECT uuid FROM biz_transaction_amount WHERE pos_sn = #{posSn} and amnount = #{amount} and host_date = #{hostDate} ")
    SynchronizationDataDTO selectTransaction(@Param("posSn") String posSn, @Param("amount") String amount, @Param("hostDate") String hostDate);

    /**
     * @author: haoran
     * @date: 2022/8/1
     * 方法名称： 通过uuid查询交易通知表id
     */
    @Select("SELECT id FROM biz_transaction_amount WHERE uuid = #{uuid} ")
    Long selectAndInsertId(String uuid);

    /**
     * @author: haoran
     * @date: 2022/8/1
     * 方法名称： 新增交易流水通知表信息
     */
    void insert(BizTransactionEntity bizTransactionEntity);

    List<TransactionRecordDTO> findTransactionRecordNew(TransactionRecord transactionRecord);

    List<BizTransactionAmountEntityVO> selectBizMerchTransactionsByCode(BizMerchantInfo bizMerchantInfo);

    BizMerchantInfo selectMerchantInfo(BizMerchantInfo bizMerchantInfo);

    @Select("select count(1) from biz_no_user where user_id=#{userId}")
    int selectNoUser(Long userId);

    @Select("select silence_type from biz_transaction_amount where uuid=#{uuid} ")
    String getSilenceType(Long uuid);
    List<BizMerchTransactions> getOnlineDetail(TransDataDTO transDataDTO);
    BizMerchTransactions getOnlineTransDetailCount(TransDataDTO transDataDTO);

    BizMerchant selectMerchant(Long userId);

    @Select("SELECT id FROM biz_transaction_amount WHERE pos_sn = #{pos_sn} and amnount = #{amnount} and host_date = #{host_date}")
    Long selectAndInsertIdForClosure(@Param("pos_sn") String pos_sn,@Param("amnount") String amnount,@Param("host_date") String host_date);

    List<BizTransactionAmountThisMonth> getTodayData();

    @InterceptorIgnore(blockAttack = "true")
    void cleanTodayTransData();

    Date selectLastTransData(String posCode);

    BigDecimal getTransAmount(@Param("merchantNo") String merchantNo,@Param("activeTime") String activeTime, @Param("expireDate")String expireDate);
}
